<template>
  <el-card 
    class="stat-card" 
    :class="{ 'clickable': clickable }"
    @click="handleClick"
    shadow="hover"
  >
    <div class="stat-content">
      <div class="stat-icon" :style="{ background: iconBg }">
        <el-icon><component :is="icon" /></el-icon>
      </div>
      <div class="stat-info">
        <div class="stat-value">{{ displayValue }}</div>
        <div class="stat-label">{{ label }}</div>
      </div>
      <div v-if="quickActions && quickActions.length > 0" class="stat-actions">
        <el-dropdown trigger="click" @command="handleAction">
          <el-button type="text" class="action-btn" @click.stop>
            <el-icon><MoreFilled /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item 
                v-for="action in quickActions" 
                :key="action.key"
                :command="action.key"
              >
                <el-icon v-if="action.icon"><component :is="action.icon" /></el-icon>
                {{ action.label }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <div v-if="loading" class="stat-loading">
      <el-icon class="is-loading"><Loading /></el-icon>
    </div>
  </el-card>
</template>

<script setup>
import { computed } from 'vue'
import { MoreFilled, Loading } from '@element-plus/icons-vue'

const props = defineProps({
  label: {
    type: String,
    required: true
  },
  value: {
    type: [Number, String],
    default: 0
  },
  icon: {
    type: Object,
    required: true
  },
  iconBg: {
    type: String,
    default: '#409EFF'
  },
  clickable: {
    type: Boolean,
    default: false
  },
  quickActions: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  formatter: {
    type: Function,
    default: null
  }
})

const emit = defineEmits(['click', 'action'])

const displayValue = computed(() => {
  if (props.formatter) {
    return props.formatter(props.value)
  }
  if (typeof props.value === 'number') {
    return props.value.toLocaleString()
  }
  return props.value
})

const handleClick = () => {
  if (props.clickable) {
    emit('click', props.value)
  }
}

const handleAction = (command) => {
  emit('action', command, props.value)
}
</script>

<style scoped>
.stat-card {
  position: relative;
  cursor: default;
  transition: all 0.3s;
}

.stat-card.clickable {
  cursor: pointer;
}

.stat-card.clickable:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.stat-content {
  display: flex;
  align-items: center;
  position: relative;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stat-icon .el-icon {
  font-size: 24px;
  color: white;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  line-height: 1.2;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.stat-actions {
  position: absolute;
  top: 0;
  right: 0;
}

.action-btn {
  padding: 4px;
  color: #909399;
}

.action-btn:hover {
  color: #409EFF;
}

.stat-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.9);
  border-radius: 4px;
  padding: 10px;
}

.stat-loading .el-icon {
  font-size: 20px;
  color: #409EFF;
}
</style>

